package ui.helpers;

import core.Database;
import core.DatabaseTable;
import core.TableColumn;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import ui.ApplicationWindow;
import ui.ColumnActionsPanel;

/**
 *
 * @author Sakis Vtdk <sakis.vtdk@gmail.com>
 */
public class EditColumnListener implements ActionListener 
{

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		//get the source (i.e. button)
		JButton button = (JButton) e.getSource();

		//get the parent of the button (i.e. the form) to have access to the components
		ColumnActionsPanel columnActionsPanel = (ColumnActionsPanel) button.getParent();

		Database db = Database.getDatabase();
		DatabaseTable tbl = db.getTable(columnActionsPanel.getParentTableId());
		TableColumn column = columnActionsPanel.getTableColumn();
		TableColumn.DataType selectedDataType = TableColumn.DataType.valueOf(columnActionsPanel.getSelectedType());

		//handle renaming
		//if the column name has been changed
		if(!columnActionsPanel.getNameTextFieldText().equals(column.getName()))
		{
			try
			{
				tbl.renameColumn(column.getId(), columnActionsPanel.getNameTextFieldText());
			}
			catch(Exception exc)
			{
				JOptionPane.showMessageDialog(null, "There is already a column named '"+ columnActionsPanel.getNameTextFieldText() +"'. Try another", "Error!", JOptionPane.ERROR_MESSAGE);
				log.error("Error while trying to rename the column. The error returned is: "+ exc.getMessage());
			}
		}

		//handle data type change
		//if the type has been changed
		if(!selectedDataType.toString().equals(column.getDataType(false)))
		{
			//TODO: do some checking between type convertion for possible data loss?
			tbl.changeColumnDataType(column.getId(), selectedDataType);
		}

		//handle data size change
		Integer size = column.getSize();
		String sizeText = columnActionsPanel.getSizeTextFieldText();
		if(size != null && selectedDataType.hasSize() && !sizeText.equals(size.toString()))
		{
			tbl.changeColumnDataTypeSize(column.getId(), columnActionsPanel.getSizeTextFieldText());
		}

		//handle the set of the default value
		if(!column.getDefaultValue().equals(columnActionsPanel.getDefaultValueTextFieldText()))
		{
			String defaultValueText = columnActionsPanel.getDefaultValueTextFieldText();

			//if the type is numeric and the default value is not
			if(selectedDataType.isNumeric() && !defaultValueText.matches("/\\d+/"))
			{
				JOptionPane.showMessageDialog(null, "The selected data type accepts only numerical values. Try another", "Error!", JOptionPane.ERROR_MESSAGE);
				log.error("Error while trying to set '"+ defaultValueText +"' as a default value for the data type '"+ selectedDataType.toString() +"'.");
			}
			else
			{
				tbl.changeColumnDefaultValue(column.getId(), columnActionsPanel.getDefaultValueTextFieldText());
			}
		}

		//handle the set of AutoIncrement
		if(selectedDataType.isAutoIncrementable() && (column.isAutoIncrement() != columnActionsPanel.isAutoIncrement()))
		{
			tbl.changeColumnAutoIncrement(column.getId(), columnActionsPanel.isAutoIncrement());
		}
		
		//handle the set of primary key
		if(column.isPrimaryKey() != columnActionsPanel.isPrimaryKey())
		{
			if(tbl.hasPrimaryKey() && (tbl.getPrimaryKeyId() != column.getId()))
			{
				//drop the existing primary key
				tbl.changeColumnPrimaryKey(tbl.getPrimaryKeyId(), Boolean.FALSE);
			}

			tbl.changeColumnPrimaryKey(column.getId(), columnActionsPanel.isPrimaryKey());
		}
		//TODO: handle the rest of the changes


		ApplicationWindow.getApplicationWindow().refreshItems();
	}

	private static Logger log = Logger.getLogger(EditColumnListener.class);
}
